<template>
  <div>
    <header>
      <section>
        <p>ToDoList</p>
        <input
          @keydown="add"
          v-model="value"
          type="text"
          placeholder="添加ToDo"
          id="add"
        />
      </section>
    </header>

    <main>
      <section>
        <!-- 正在进行的列表 -->
        <div class="doList">
          <div class="title">
            <h2>正在进行</h2>
            <span class="todoCount">{{ unSelectedList.length }}</span>
          </div>

          <List :list="unSelectedList" @delete="deleteTask"></List>
        </div>

        <!-- 已完成的列表 -->
        <div class="doneList">
          <div class="title">
            <h2>已完成</h2>
            <span class="doneCount">{{ selectedList.length }}</span>
          </div>

          <List :list="selectedList" @delete="deleteTask"></List>
        </div>
      </section>
    </main>
  </div>
</template>

<script>
import List from '@/components/list.vue';

export default {
  components: {
    List,
  },
  data() {
    return {
      value: '',
      todoList: [],
    };
  },
  methods: {
    add(e) {
      if (e.keyCode !== 13) return;

      this.todoList.push({
        id: this.todoList.length,
        task: this.value,
        checked: false,
      });

      this.value = '';
    },
    deleteTask(task) {
      const index = this.todoList.indexOf(task);
      this.todoList.splice(index, 1);
    },
  },
  computed: {
    selectedList() {
      return this.todoList.filter(task => task.checked === true);
    },
    unSelectedList() {
      return this.todoList.filter(task => task.checked === false);
    },
  },
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
  list-style: none;
  text-decoration: none;
}

body {
  background-color: #cdcdcd;
}

header {
  height: 50px;
  background: rgba(47, 47, 47, 0.98);
}

section {
  width: 600px;
  margin: 0 auto;
}

header section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 50px;
}

header section p {
  color: #ddd;
  font-size: 24px;
  cursor: pointer;
}

header section input {
  width: 50%;
  text-indent: 10px;
  border-radius: 5px;
  box-shadow: 0 1px 0 rgb(255 255 255 / 24%), 0 1px 6px rgb(0 0 0 / 45%) inset;
  border: none;
  outline: none;
  padding: 5px 10px;
}

.title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title h2 {
  margin: 20px 0;
}

.title span {
  padding: 0 5px;
  height: 20px;
  border-radius: 20px;
  background: #e6e6fa;
  line-height: 22px;
  text-align: center;
  color: #666;
  font-size: 14px;
}
</style>
